package com.juku2024.juku.viewmodel

import com.juku2024.juku.db.NoteRoomUtils
import com.juku2024.juku.db.entity.UnlockMoreModel
import com.juku2024.juku.request.RankDetailRequest
import com.juku2024.juku.request.RankRequest
import com.juku2024.juku.model.RankingResponse
import com.juku2024.juku.network.api.Api
import com.juku2024.juku.network.retrofit.OnRetrofitResultListener
import com.juku2024.juku.network.retrofit.RetrofitUtils
import com.juku2024.juku.request.UnlockRequest
import com.juku2024.juku.tools.Constants
import com.juku2024.juku.tools.MD5Utils
import kotlinx.coroutines.*

class UnlockViewModel : BaseViewModel() {

    fun getUnlockList(callback: OnRetrofitResultListener<UnlockMoreModel>) {
        var apiVersion = 0L
        doBaseHttp({
            val versionResponse = getVersion(Constants.API_TYPE_UNLOCK)
            apiVersion = versionResponse.apiVersion
            val dao = NoteRoomUtils.getInstance().unlockMoreDao()
            var model: UnlockMoreModel? = null
            if (!versionResponse.isUpgrade) {
                model = withContext(Dispatchers.IO) { dao.query() }
                if (model != null) {
                    callback.onSuccess(model)
                } else {
                    dao.delete()
                }
            }
            return@doBaseHttp model
        }, {
            val appId = getAppId()
            val timestamp = getSecondTimestamp()
            val sign = MD5Utils.getMD5String(apiVersion, appId, getAppKey(), timestamp, getExtra())
            val request = UnlockRequest(apiVersion.toInt(), appId, timestamp, sign)
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).getUnlock(getCommonRequest(request))
        }, object: OnRetrofitResultListener<UnlockMoreModel>{

            override fun onSuccess(t: UnlockMoreModel) {
                if (isNotActive()) return
                callback.onSuccess(t)
            }

            override fun onFailed(msg: String) {
                if (isNotActive()) return
                callback.onFailed(msg)
            }

        })
    }

    /**
     * 获取榜单导航
     */
    fun getRankingNavList(success: (model: RankingResponse) -> Unit, fail: (msg: String) -> Unit){
        doBaseHttp({
            val appId = getAppId()
            val timestamp = getSecondTimestamp()
            val sign = MD5Utils.getMD5String(appId, getAppKey(), timestamp, getExtra())
            val request = RankRequest(appId, timestamp, sign)
            // 查当前数据表是否有数据
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).getRankingNavList(getCommonRequest(request))
        }, object : OnRetrofitResultListener<RankingResponse> {
            override fun onSuccess(t: RankingResponse) {
                success(t)
            }

            override fun onFailed(msg: String) {
                fail(msg)
            }
        })
    }

    /**
     * 获取榜单导航对应的列表
     */
    fun getRankingNavDetailList(id: Int, success: (model: RankingResponse.RankTabModel) -> Unit, fail: (msg: String) -> Unit){
        doBaseHttp({
            val appId = getAppId()
            val timestamp = getSecondTimestamp()
            val sign = MD5Utils.getMD5String(
                id,
                appId,
                getAppKey(),
                timestamp,
                getExtra()
            )
            val request = RankDetailRequest(appId, timestamp, id, sign)
            // 查当前数据表是否有数据
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).getRankingNavDetailList(id, getCommonRequest(request))
        }, object : OnRetrofitResultListener<RankingResponse.RankTabModel> {
            override fun onSuccess(t: RankingResponse.RankTabModel) {
                success(t)
            }

            override fun onFailed(msg: String) {
                fail(msg)
            }
        })
    }
}